using System.Xml.Xsl;
using Microsoft.VisualStudio.CodeCoverage;
using System.IO;

namespace CoverageRunner
{
    public class CoverageReportCreator
    {
        private readonly string vstsCoverageOutputFile;
        private readonly string symbolsPath;
        private readonly string exePath;
        private readonly string coverageOutputXmlFilename;
        private readonly string transformXsltFilename;
        private readonly string transformOutputFilename;

        public CoverageReportCreator(string vstsCoverageOutputFile, string symbolsPath, string exePath, string coverageOutputXmlFilename, string transformXsltFilename, string transformOutputFilename)
        {
            this.vstsCoverageOutputFile = vstsCoverageOutputFile;
            this.symbolsPath = symbolsPath;
            this.exePath = exePath;
            this.coverageOutputXmlFilename = coverageOutputXmlFilename;
            this.transformXsltFilename = transformXsltFilename;
            this.transformOutputFilename = transformOutputFilename;
        }

        public string Create()
        {
            CoverageInfo coverageInfo = GetCoverageInfo(vstsCoverageOutputFile, symbolsPath, exePath);

            if (string.IsNullOrEmpty(coverageOutputXmlFilename))
                return "No coverage output Xml file specified, no reports were produced";

            WriteCoverageOutputAsXml(coverageInfo, coverageOutputXmlFilename);

            if (string.IsNullOrEmpty(transformXsltFilename))
                return "No XSLT file specified for report transformation, no final output produced";
            if (!File.Exists(transformXsltFilename))
                return string.Format("The XSLT file specified '{0}' was not found, no final output produced", transformXsltFilename);

            WriteCoverageOutputWithTransfrom(coverageOutputXmlFilename, transformXsltFilename,
                                                 transformOutputFilename);

            return "Reports created";
        }

        private static CoverageInfo GetCoverageInfo(string vstsCoverageOutputFile, string symbolsPath, string exePath)
        {
            CoverageInfoManager.SymPath = symbolsPath;
            CoverageInfoManager.ExePath = exePath;
            return CoverageInfoManager.CreateInfoFromFile(vstsCoverageOutputFile);
        }

        private static void WriteCoverageOutputWithTransfrom(string coverageOutputXmlFilename, string transformXsltFilename, string transformOutputFilename)
        {
            XslCompiledTransform transformer = new XslCompiledTransform();
            transformer.Load(transformXsltFilename);
            transformer.Transform(coverageOutputXmlFilename, transformOutputFilename);
        }

        private void WriteCoverageOutputAsXml(CoverageInfo coverageInfo, string coverageOutputXmlFilename)
        {
            CoverageDS data = coverageInfo.BuildDataSet(null);
            data.ExportXml(coverageOutputXmlFilename);
        }

    }
}